package nps.pay.servlet;

import nps.pay.config.NpsConfig;
import nps.pay.utils.HttpUtils;
import nps.pay.utils.SignUtils;
import okhttp3.Response;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class SubstituteServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderNo = req.getParameter("orderNo");
        String money = req.getParameter("money");
        String bankCode = req.getParameter("bankCode");
        String ACCOUNT_NO = req.getParameter("ACCOUNT_NO");
        String BANK_NAME = req.getParameter("BANK_NAME");
        String ACCOUNT_NAME = req.getParameter("ACCOUNT_NAME");

        Map<String, String> dataMap = fullRequestData(orderNo, money, bankCode, ACCOUNT_NO, BANK_NAME, ACCOUNT_NAME);
        Response response = HttpUtils.sendRequest(NpsConfig.PAY_URL, dataMap);
        String result = response.body().string();
        System.out.println("代付响应结果：" + result);
        Document reqDoc = null;
        try {
            reqDoc = DocumentHelper.parseText(result);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element rootEl = reqDoc.getRootElement();
        String RESP_CODE = rootEl.elementText("RESP_CODE");
        String RESP_DESC = rootEl.elementText("RESP_DESC");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json; charset=utf-8");
        PrintWriter writer = resp.getWriter();
        Map<String, String> map = new HashMap<>();

        if ("0000".equals(RESP_CODE) || "00".equals(RESP_CODE)) {
            System.out.println("代付成功");
            map.put("status", "success");
        } else {
            System.err.println("代付失败:" + RESP_DESC);
            map.put("status", "error");
            map.put("msg", RESP_DESC);
        }
        writer.write(map.toString());
//http://localhost/substituteServlet?orderNo=123456789&money=1.00&ACCOUNT_NO=6227000062751278199&bankCode=01050000&BANK_NAME=建设&ACCOUNT_NAME=测试
    }

    private Map<String, String> fullRequestData(String orderNo, String money, String bankCode, String ACCOUNT_NO, String BANK_NAME, String ACCOUNT_NAME) {
        Map<String, String> dataMap = new LinkedHashMap<>();
        dataMap.put("ORDER_ID", orderNo);//订单号
        dataMap.put("ORDER_AMT", money);//元
        dataMap.put("ORDER_TIME", new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));//YYYYMMDDHHMMSS
        dataMap.put("USER_TYPE", "02");//
        dataMap.put("USER_ID", NpsConfig.USER_ID);//
        dataMap.put("SIGN_TYPE", "03");//
        dataMap.put("BUS_CODE", "1006");//
        dataMap.put("BANK_CODE", bankCode);//
        dataMap.put("ACCOUNT_NO", ACCOUNT_NO);//银行卡号
        dataMap.put("SIGN", SignUtils.sign(dataMap));
        //上面的需要参与签名
        dataMap.put("BANK_NAME", BANK_NAME);//商户名称
        dataMap.put("ACCOUNT_NAME", ACCOUNT_NAME);//
        return dataMap;
    }
}
